<?php
/**
 * Created by PhpStorm.
 * User: lxq
 * Date: 2018/3/22
 * Time: 9:37
 */
class foodModel extends Model {

    public function __construct()
    {
        parent::__construct('food');
    }

    /**
     * 获取列表分页
     * @param array $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public function getFoodListPage($condition = array(), $field = '*', $page = 0, $order = 'fd_id desc'){
        return $this->field($field)->where($condition)->page($page)->order($order)->select();
    }

    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
    public function getFoodInfo($condition = array(), $field = '*')
    {
        return $this->field($field)->where($condition)->find();
    }

    /**
     * 插入一条记录
     * @param array $params
     * @return mixed
     */
    public function addFood($params)
    {
        return $this->insert($params);
    }

    /**
     * 更新记录
     * @param array $condition
     * @param array $params
     * @return mixed
     */
    public function updateFood($condition, $params)
    {
        return $this->where($condition)->update($params);
    }

    /**
     * 删除一条记录
     * @param array $condition
     */
    public function deleteFood($condition)
    {
        return $this->where($condition)->delete();
    }
    /**
     * 获取记录数组
     * @param array $condition
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public function getFoodList($condition = array(), $field = '*', $order = 'fd_id desc'){
        return $this->field($field)->where($condition)->order($order)->select();
    }

    /**
     * 增加销量
     * @param int $tk_id
     * @param int $num
     */
    public function addFoodSales($tk_id,$num=1)
    {
        return $this->where(array('fd_id'=>$tk_id))->setInc('sales',$num);
    }

    /**
     * 减少库存
     * @param int $fd_id
     * @param int $num
     */
    public function reduceFoodStock($fd_id,$num=1)
    {
        return $this->where(array('fd_id'=>$fd_id))->setDec('stock',$num);
    }

    /**
     * 增加库存
     * @param int $num
     * @param int $fd_id
     */
    public function addFoodStock($fd_id,$num=1)
    {
        return $this->where(array('fd_id'=>$fd_id))->setInc('stock',$num);
    }



    /**
     * 取得数量
     * @param array $condition
     */
    public function getFoodCount($condition) {
        return $this->where($condition)->count();
    }

    /**
     * 获取所有商品分类的城市ID（去重复）
     */
    public function getFoodAreaId()
    {
        $city_list =  $this->where(array('fd_status'=>1))->field('fd_area_id')->group('fd_area_id')->select();
        /*if($city_list && is_array($city_list)){
            $arr = array();
            foreach($city_list as $key=>$value){
                $arr[$value['ht_city_id']][] =$value['ht_area_id'];
            }
            return $arr;

        }*/
        return $city_list;
    }

    public function get_status_count($condition){
        $field = "count(fd_status=1 or null ) as 'sale_count', count(fd_status=0 or null ) as 'un_sale_count' ";
        return $this->field($field)->where($condition)->find();
    }


}